<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>阶段强势股</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">						
			.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}
			table{
			width:380px; 
			border-collapse:collapse; 
			text-align:center;
			}
			td,th{border:1px solid #696969}					
		</style>
	</head>
	<body>		
		<div id="towrite" class="fixedBox"></div>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script>
			var bkcolor = $("body").css("backgroundColor");
			var day = new Date();			
			var tt = "";			
			window.onload = function() {								
			getSource();
			changeStyle();					
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource();					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {					
				time_range("9:00","15:15")																
				},
				60000);
			})
			
			function changeStyle() {
				var obj1 = document.getElementById("towrite");				
				obj1.style.left= "0px"; 				
				obj1.style.width= "480px";	
				obj1.style.height= "620px";		
				obj1.style.fontSize="12px";							
			}					
			
			var trid = "";
			function changeCss(obj,hid,id){						
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){														
					tlist[i].style.backgroundColor = bkcolor; 					
				}
				trid = hid;
				var tr = document.getElementById(hid);				
				tr.style.backgroundColor = "#483D8B";						
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){						
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头
							if(this.id != trid){  //过滤选中行
								this.style.background=bkcolor;
							}else{
								this.style.background="#483D8B";
							}
						}						
					}					
				}
			}
			
			
			function createXmlHttp() {
				if (window.XMLHttpRequest) {
					xmlHttp1 = new XMLHttpRequest();						
				} else {
					xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");								
				}
			}
			
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
			return new Date(parseInt(nS) * 1000).Format("hh:mm:ss")}
			
			function keepTwoDecimal(num) {  
               var result = parseFloat(num);  
               if (isNaN(result)) {  
                   //alert('传递参数错误，请检查！');  
                   return false;  
               }  
               result = Math.round(num * 100) / 100;  
               return result;  
			};
			
			function UnUnicode(str) {
				return unescape(str.replace(/\\/g, "%"));
			}
			
			function getSource() {				
				if(k == 0){
					div = document.getElementById('towrite');
					var qh = "<a style='position:absolute; right:30px;' href='javascript:void(0)' onclick='clearstorage()'>" + "清除缓存" + "</a>"; //清除区间前缓存
					div.innerHTML = '<font size="3">阶段强势股（' + n + '日）<font size="1">&nbsp;&nbsp;&nbsp;&nbsp;' + qh + '</font></font>';				
					div.innerHTML += "<p>请稍等……</p>";		
				}				
				var url = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=RealRankingInfo&st=30&apiv=w21&Type=1&c=ZhiShuRanking&PhoneOSNew=1&ZSType=7&"; 				
				createXmlHttp();				
				xmlHttp1.onreadystatechange = writeSource1;
				xmlHttp1.open("GET", url, true);
				xmlHttp1.send(null);				
			}
			
			function clearstorage(){
				var st = localStorage.getItem('lbstartrq');				
				var stm = new Date(st).getTime();
				var day = new Date();
				day.setTime(day.getTime());			
				for (i = 1; i < 300; i++){																		
					day.setTime(day.getTime() - 24*60*60*1000);
					var tt = day.Format("yyyy-MM-dd");
					if(stm > day.getTime() && localStorage.getItem('lb' + tt) != null){						
						localStorage.removeItem('lb' + tt); //清除缓存localStorage
					}
				}
				alert('清除完成');
			}
			
			var url11 = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=100&apiv=w21&Type=4&c=HomeDingPan&PhoneOSNew=1&PidType=5&"; 				
			var url21 = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=100&apiv=w21&Type=4&c=HomeDingPan&PhoneOSNew=1&PidType=4&"; 				
			var url31 = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=100&apiv=w21&Type=4&c=HomeDingPan&PhoneOSNew=1&PidType=3&"; 				
			var url41 = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=100&apiv=w21&Type=4&c=HomeDingPan&PhoneOSNew=1&PidType=2&"; 				
			var url51 = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=100&apiv=w21&Type=4&c=HomeDingPan&PhoneOSNew=1&PidType=1&";
			
			var url12 = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=1000&apiv=w26&Type=4&c=HisHomeDingPan&Index=0&PidType=5&Day=";
			var url22 = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=1000&apiv=w26&Type=4&c=HisHomeDingPan&Index=0&PidType=4&Day=";
			var url32 = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=1000&apiv=w26&Type=4&c=HisHomeDingPan&Index=0&PidType=3&Day="; 				
			var url42 = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=1000&apiv=w26&Type=4&c=HisHomeDingPan&Index=0&PidType=2&Day=";
			var url52 = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&a=DailyLimitPerformance&st=1000&apiv=w26&Type=4&c=HisHomeDingPan&Index=0&PidType=1&Day=";
			
			var datas = "";
			var datas2 = "";
			var k = 0;
			var zxs = "";	
			function writeSource1() { 
				if (xmlHttp1.readyState == 4) { 																
					var data = xmlHttp1.responseText
					//document.write(data);
					
					//tt = data.match(/\"Day\":\[\"(\S*)\"\],/)[1];	//最后交易日										
					tt = data.match(/"Day":\["(\S*)/)[1].split('"]')[0];
					
					var url1 = url11;
					var url2 = url21;
					var url3 = url31;
					var url4 = url41;
					var url5 = url51;					
					var day1 = new Date();
					var ts = day1.Format("yyyy-MM-dd");
					//alert(ts);
					if(tt != ts){
						url1 = url12 + tt;
						url2 = url22 + tt;
						url3 = url32 + tt;
						url4 = url42 + tt;
						url5 = url52 + tt;
					}
					var url = "";
					var p = 0;
					for (i = 1; i < 6 ; i++)
					{	
						if(i == 1){
							url = url1;
							p = 5;
						}
						if(i == 2){
							url = url2;
							p = 4;
						}
						if(i == 3){
							url = url3;
							p = 3;
						}
						if(i == 4){
							url = url4;
							p = 2;
						}
						if(i == 5){
							url = url5;
							p = 1;
						}						
						getSourcehq(url , p);						
					}
					var datas1 = datas; //最后交易日数据
					
					if(k == 0){  //刷新时才调用
						k = 1;
						datas = "";					
						day = new Date(tt);
						gethis(); //交易日向前推算
						datas2 = datas;  //历史交易日数据						
					}
					datas = datas1 + datas2;
					//document.write(datas);
					writeSource2();	
				}				
			}
											
			var n = 20; //统计区间，交易日天数
			function gethis() {						
				for (j = 1; j < n; j++){											
					gethisdate();					
				}
				//alert(tt)
				localStorage.setItem('lbstartrq', tt);  //起始日期写入缓存
			}
			
			var onedaysdata = "";		//单日数据	
			function gethisdate() {		//历史交易日推算
				onedaysdata = "";
				//day.setTime(day.getTime());			
				for (i = 1; i < 100; i++){	//节假日前推																		
					day.setTime(day.getTime() - 24*60*60*1000);
					tt = day.Format("yyyy-MM-dd");	
					if(localStorage.getItem('lb' + tt) != null) {   //判断有无缓存数据							
						datas += localStorage.getItem('lb' + tt);    //取缓存数据						
						break;	
					}else{ 					
						var url = "https://apphis.longhuvip.com/w1/api/index.php?Order=1&a=RealRankingInfo&st=30&apiv=w26&Type=1&c=ZhiShuRanking&ZSType=7&Date=" + tt + "&"; 							
						var xhr = new XMLHttpRequest();	
						xhr.open("GET", url, false);//同步 
						xhr.send(null)																										
						var data = xhr.responseText;					
						if(data.indexOf("MYSQL数据库链接出错!") == -1 && data.indexOf('"list":[]') == -1){						
							var url = "";
							url1 = url12 + tt;
							url2 = url22 + tt;
							url3 = url32 + tt;
							url4 = url42 + tt;
							url5 = url52 + tt;
							for (i = 1; i < 6 ; i++){	
								if(i == 1){
									url = url1;
								}
								if(i == 2){
									url = url2;
								}
								if(i == 3){
									url = url3;
								}
								if(i == 4){
									url = url4;
								}
								if(i == 5){
									url = url5;
								}							
								getSourcehq(url , 0);							
							}	
							
							localStorage.setItem('lb' + tt, onedaysdata);  //单日数据写入缓存														
							break;						
						}
					}
				}				
			}
						
			function getSourcehq(url , p){	//获取数据
				if (window.XMLHttpRequest) {
					xmlHttp = new XMLHttpRequest();								
				} else {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");							
				}
				xmlHttp.open("GET", url, false);  //同步方式
				xmlHttp.send(null);				
				
				var data = xmlHttp.responseText					
				data = data.replace("\[\[\]","\[\]");
				//document.write(data);
				if (data.indexOf('\"' + 'info' + '\"' + ':\[\]') == -1 ) { 					
					//data = data.replace(/ /g,"").match(/(\S*)\]\],\"/)[1] + ']]';
					data = data.replace(/ /g,"").match(/\{\"info\":\[\[(\S*)\]\],\"/)[1] + '],';
					data = data.replace(/(\[\").*?(?=\])/g,function(word){ str = word.match(/\[(\S*)/)[1]; return '{"code":' + str.split(',')[0] + ',"name":' + str.split(',')[1] + ',"ztyy":' + str.split(',')[5] + ',"lb":"' + p + '"';});										
					//document.write(data);
					datas += data;
					onedaysdata += data;
				}																
			}	
			
			var dms = "";
			var bb = "";
			var datasp = "";
			function writeSource2() {
				var div = document.getElementById("towrite");
				var qh = "<a style='position:absolute; right:30px;' href='javascript:void(0)' onclick='clearstorage()'>" + "清除缓存" + "</a>";
				div.innerHTML = '<font size="3">阶段强势股（' + n + '日）<font size="1">&nbsp;&nbsp;&nbsp;&nbsp;' + qh + '</font></font><br><br>';				
				var ss = datas.split("],{");
				for (i = 0; i < ss.length ; i++){
					var dm = ss[i].match(/\"code\":\"(\S*)\",\"name/)[1];
					var cs = 0; 					
					if(dms.indexOf(dm) == -1){						
						dms += dm + ";";
						var cc = datas.split(dm);
						cs = cc.length - 1;
						if(cs > 1){
							bb += ss[i] + ',"cs":' + cs + '},{';
						}						
					}
				}				
				datasp = bb + '}';
				datasp = "[" + datasp.replace("},{}","}") + "]";
				//getdata("1");
				
				datasp = datasp.replace(/\"lb\":\"5\"/g,'"lb":"4板以上"');
				var json = JSON.parse(datasp);					
					
				//创建table					
				var table=document.createElement("table");										
				var thead=document.createElement("thead");
				table.appendChild(thead);					
				var tr=document.createElement("tr");
				thead.appendChild(tr);					
				
				var m = 0;
				var g = 0;
				var b = 0;
				var gl = "连板";  //高亮
				for(var key in json[0]){  //表头
					key = key.replace('code','代码').replace('name','名称').replace('ztyy','涨停原因').replace('lb','连板').replace('cs','涨停次数');					
					var th=document.createElement("th");						
					th.innerHTML=key;						
					tr.appendChild(th);
					if(gl == key.replace("%","")){
						g = m;
						th.style.color = "#FF6633";	
					}					
					m++;
				}							
				var tbody=document.createElement("tbody");					
				table.appendChild(tbody);
				
				//遍历json
				for(var i = 0; i < json.length; i++){					
					var tr=document.createElement("tr");						
					var p = 1;
					var dm = "";
					var mc = "";
					for(var key in json[i]){ //行元素遍历						
						var td=document.createElement("td");							
						
						var rc = json[i][key];
						if(p == 1){  //代码列
							dm = rc;
							tr.id = dm;
						}else if(p == 5){
							rc = rc + "/" + n;
						}					
						td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"" + dm + "\",\"gg\")'>" + rc + "</a>";						
						if(p == g + 1){							
							td.getElementsByTagName("a")[0].style.color = "#FF6633";
						}						
						tr.appendChild(td);							
						p++;
					}													
					tbody.appendChild(tr);																
				}					
													
				div.appendChild(table);
				table.id = "gg";
				
				var table=document.getElementsByTagName("table")[0];
				makeSortable(table);			
				hover();					
			}
			
			function makeSortable(table) {
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){												
							showCol(n);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								if(headtxt.indexOf("名称") != -1 || headtxt.indexOf("涨停原因") != -1 || headtxt.indexOf("连板") != -1){
									val1 = val1;									
									val2 = val2;									
								}else{																	
									if(val1.indexOf('\/')>-1){
										val1 = val1.split('\/')[0];
										val1 = parseFloat(val1);
									}else{
										val1 = parseFloat(val1);									
									}
									
									if(val2.indexOf('\/')>-1){
										val2 = val2.split('\/')[0];
										val2 = parseFloat(val2);
									}else{
										val2 = parseFloat(val2);									
									}
								}
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
							});
							if(flag){
								rows.reverse();
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;
						}
					}(i));
				}
			}
			
			function showCol(iCol){			
				var oTable = document.getElementsByTagName('table')[0];
				for (i=0;i < oTable.rows.length; i++){										
					for (j=0;j < oTable.rows[0].cells.length; j++){
						oTable.rows[i].cells[j].style.color = "#E5E5E5";								
					}
					oTable.rows[i].cells[iCol].style.color = "#FF6633";					
				}

				otagsA = oTable.getElementsByTagName("a");  //a变色
				for (i = 0; i < otagsA.length; i++) {
					otagsA[i].style.color = "#E5E5E5";					
					if(otagsA[i].id == iCol + 1){
						otagsA[i].style.color = "#FF6633";
					}
				}								
			}
			
		</script>	
	</body>
</html>